package com.hhkj.generator.common;

import cn.hutool.core.util.StrUtil;
import com.hhkj.generator.entity.ExcelFieldItem;
import com.hhkj.generator.entity.FieldItem;
import com.hhkj.generator.entity.TableItem;
import com.hhkj.generator.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class FieldList2TableItemParser {

    private static final String[] filterFieldArray = new String[]{"id", "del_flag", "create_by", "create_time", "update_by", "update_time"};

    public List<TableItem> parse(List<ExcelFieldItem> fieldList) {
        List<TableItem> rsList = new ArrayList<>();

        List<List<ExcelFieldItem>> fieldListList = split(fieldList);

        for (List<ExcelFieldItem> oneList : fieldListList) {
            ExcelFieldItem fieldItem = oneList.get(0);
            TableItem table = new TableItem();
            table.setTableCode(fieldItem.getTableCode());
            table.setTableName(fieldItem.getTableName());
            table.setTableDesc(fieldItem.getTableDesc());
            table.setPackageCode(fieldItem.getPackageCode());
            String tablePrefix = fieldItem.getTablePrefix();
            table.setTablePrefix(tablePrefix);
            if (tablePrefix == null || tablePrefix.length() == 0){
                String tableCode = table.getTableCode();
                if (tableCode.startsWith("x_")) {
                    tablePrefix = "x_";
                    table.setTablePrefix(tablePrefix);
                    table.setTableRealName(StrUtil.removePrefix(tableCode, "x_"));
                }
            }
            String objectCode = getObjectCode(fieldItem.getTableCode(), tablePrefix);
            table.setObjectCode(objectCode);

            List<FieldItem> oneFieldList = new ArrayList<>();
            for (ExcelFieldItem oneFieldItem : oneList) {
                String sqlCode = oneFieldItem.getCode();
                if (isFilter(sqlCode)){
                    continue;
                }
                FieldItem oneItem = FieldItem.toFieldItem(oneFieldItem);
                oneFieldList.add(oneItem);
            }
            table.setFieldList(oneFieldList);

            rsList.add(table);
        }

        return rsList;
    }

    private List<List<ExcelFieldItem>> split(List<ExcelFieldItem> fieldList) {
        List<List<ExcelFieldItem>> rsList = new ArrayList<>();
        List<ExcelFieldItem> oneList = new ArrayList<>();

        for (ExcelFieldItem fieldItem : fieldList) {
            String tableCode = fieldItem.getTableCode();
            if (tableCode != null && !tableCode.trim().isEmpty()) {
                if (oneList.size() > 0) {
                    rsList.add(oneList);
                }
                oneList = new ArrayList<>();
            }
            oneList.add(fieldItem);
        }
        rsList.add(oneList);

        return rsList;
    }

    private String getObjectCode(String tableCode, String tablePrefix) {
        String rs = tableCode;
        if (tablePrefix != null && tablePrefix.length() > 0) {
            rs = tableCode.substring(tablePrefix.length());
        }
        rs = StringUtil.firstCharUpperCase(StringUtil.camelName(rs));
        return rs;
    }

    private boolean isFilter(String code) {
        for (String str : filterFieldArray) {
            if (str.equals(code)){
                return true;
            }
        }

        return false;
    }

}
